package main.java.org.service.serviceImpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import main.java.Exam.pojo.Entity.Log;
import main.java.Exam.pojo.Entity.Questtion;
import main.java.Exam.pojo.tableDTO.QuesttionDTO;
import main.java.Exam.pojo.tableVO.PageResultVO;
import main.java.Exam.pojo.tableVO.QuesttionVO;
import main.java.org.mapper.LogMapper;
import main.java.org.mapper.QuestionMapper;
import main.java.org.service.questionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Slf4j
@Service
public class QuestionServiceImpl implements questionService {
@Autowired
private QuestionMapper questionMapper;


      //添加题目
    @Override
    public void createquestion(QuesttionDTO questionDTO) {
        questionDTO.setCreateTime(LocalDateTime.now());
        questionMapper.insert(questionDTO);
    //    logexam("123312123","陈平安","添加题目","题目模块");
    }

    //删除题
    @Override
    public void delete(QuesttionDTO questionDTO) {
        if(questionDTO.getQuestionId()==null||questionDTO.getType()==null) {
            throw new IllegalArgumentException("questionid和type不能为空");
        }
        Integer id= questionDTO.getQuestionId();
        Integer type= questionDTO.getType();
           questionMapper.delete(id,type);

    }


    //分页查询
    @Override
    public PageResultVO<QuesttionVO> getPage(Integer pageNum, Integer pageSize) {
        //分页参数
        Page<Questtion> page = new Page<>(pageNum , pageSize);
        //执行分页查询
        Page<Questtion> resultPage = questionMapper.selectPage(page);
        ObjectMapper objectMapper = new ObjectMapper();
        //转换实体VO
        List<QuesttionVO> voList = resultPage.getRecords().stream().map(question -> {
            QuesttionVO vo = new QuesttionVO();
            BeanUtils.copyProperties(question,vo);


            try {
                String optionsStr = question.getOptions();
                if (optionsStr != null && !optionsStr.isEmpty()) {
                    //将 JSON 字符串转为 List<String>
                    List<String> optionsList = objectMapper.readValue(
                            optionsStr,
                            new TypeReference<List<String>>() {}
                    );
                    vo.setOptions(optionsList); // 赋值给 VO
                }else {
                    // 若optionsStr为空，设置默认空列表（避免null）
                    vo.setOptions(new ArrayList<>());
                }
            } catch (Exception e) {

                e.printStackTrace();
                vo.setOptions(null); // 或设置默认空列表
            }

            return vo;
        }).collect(Collectors.toList());

        //封装分页结果
        PageResultVO<QuesttionVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(resultPage.getTotal()); // 总条数
        pageVO.setCurrent(pageNum); // 当前页
        pageVO.setSize(pageSize); // 每页条数
        pageVO.setRecords(voList); // 数据列表

        return pageVO;
    }
    //修改题目
    @Override
    public void alter(QuesttionDTO questionDTO) {
       questionDTO.setUpdateTime(LocalDateTime.now());
        questionMapper.updata(questionDTO);
       // logexam("654321","eliauk","修改题目","题目模块");
    }
//查看单个题目
    @Override
    public QuesttionDTO getquestion(Integer questionId, Integer type) {
        Questtion question = questionMapper.getquestion(questionId, type);
        if (question == null) {
            return new QuesttionDTO(); // 无数据返回空 DTO
        }

        //转换为 QuesttionDTO 并处理 options 类型
        QuesttionDTO dto = new QuesttionDTO();
        // 复制其他字段
        dto.setQuestionId(question.getQuestionId());
        dto.setType(question.getType());
        dto.setScore(question.getScore());
        dto.setContent(question.getContent());
        dto.setAnswer(question.getAnswer());
        dto.setCreateTime(question.getCreateTime());
        dto.setUpdateTime(question.getUpdateTime());

        //处理 options：String → List<String>
        ObjectMapper objectMapper = new ObjectMapper();
        // 配置空字符串转为空列表（和分页查询保持一致）
        List<String> optionsList = new ArrayList<>();
        try {
            String optionsStr = question.getOptions();
            if (optionsStr != null && !optionsStr.isEmpty()) {
                // 解析 JSON 字符串为 List<String>
                optionsList = objectMapper.readValue(optionsStr, new TypeReference<List<String>>() {});
            }
        } catch (Exception e) {
            e.printStackTrace();
            optionsList = new ArrayList<>(); // 异常时返回空列表
        }
        dto.setOptions(optionsList);
        //logexam("654321","eliauk","查看题目","题目模块");
        return dto;
    }

}
